package com.yixun.qfbao.common.service.impl;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.yixun.qfbao.common.service.ApiServiceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApiServiceClientImpl implements ApiServiceClient{
    private static Logger logger= LoggerFactory.getLogger(ApiServiceClientImpl.class);
	@Override
    public String requestByGet(String url, Map<String, String> properties)throws Exception{
        int count=0;
		for (Map.Entry<String, String> entry : properties.entrySet()) {
			if(count==0){
				url=url+"?"+entry.getKey()+"="+entry.getValue();
			}else{
				url=url+"&"+entry.getKey()+"="+entry.getValue();
			}
			count++;
		}
        String content = null; 
        BufferedReader in = null;   
        try {  
            // 定义HttpClient  
            HttpClient client = new DefaultHttpClient();  
            // 实例化HTTP方法  
            HttpGet request = new HttpGet();  
            request.setURI(new URI(url));  
            HttpResponse response = client.execute(request);  
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
            StringBuffer sb = new StringBuffer("");  
            String line = "";  
            String NL = System.getProperty("line.separator");  
            while ((line = in.readLine()) != null) {  
                sb.append(line + NL);  
            }  
            in.close();  
            content = sb.toString(); 
        } finally {  
            if (in != null) {  
                try {  
                    in.close();// 最后要关闭BufferedReader  
                } catch (Exception e) {
                    logger.error("bufferedReader error: {}", e);
                }  
            }  
            
        }
		return content;
		
	}

	

	public static void main(String[] args) {
		ApiServiceClient client=new ApiServiceClientImpl();
		try {
			String result=client.requestByGet("http://wx.qiaofang.info/fxt/jsapi/sign.json", new HashMap<String,String>());
			System.out.print(result);
		} catch (Exception e) {
            logger.error("ApiServiceClientImpl error: {}", e);
		}
	}

}
